package com.ys.controller.api.storage;

import com.ys.config.MenuId;
import com.ys.entity.purchase.TcgInvoiceRegist;
import com.ys.entity.purchase.TcgInvoiceRegistItem;
import com.ys.entity.purchase.TcgPurchaseAppointOrderProductComponent;
import com.ys.entity.storage.TccLocationTransfer;
import com.ys.entity.storage.TccStockAllocate;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.purchase.TcgInvoiceRegistService;
import com.ys.service.storage.TccLocationTransferService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/*
 * 货位转移单
 */
@Controller
@RequestMapping("/api/inter/tccLocationTransfer")
public class ApiInterTccLocationTransferController {
	
	@Autowired
	private CommonApiService<TccLocationTransfer> commonApiService;
	@Autowired
	private TccLocationTransferService tccLocationTransferService;
	
	Long MENUID =  MenuId.TCC_LocationTransfer;

	@RequiresPermissions("tccLocationTransfer:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TccLocationTransfer cond, String conditions){
		
		cond = setSeachSql(cond, conditions);
	
		return commonApiService.searchLay(cond);
	}
	
	/**
	 * 新增
	 */
	@RequestMapping("/toAdd")
	@ResponseBody
	@RequiresPermissions("tccLocationTransfer:add")
	public R add(String bid, String remark, String subData) {
		try {
			return tccLocationTransferService.add(bid, remark, subData);
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return R.error(e.getMessage());
		}
	}

	/**
	 * 修改
	 */
	@RequestMapping("/toEdit")
	@ResponseBody
	@RequiresPermissions("tccLocationTransfer:add")
	public R edit(String bid, String remark, String subData) {
		try {
			return tccLocationTransferService.edit(bid, remark, subData);
		} catch (Exception e) {
			return R.error(e.getMessage());
		}
	}

	/**
	 * 流转通过
	 */
	@RequiresPermissions("tccLocationTransfer:add")
	@PostMapping("/flowPass")
    @ResponseBody
	public R flowPass(HttpServletRequest request, Long id){
		try {
    		String loginUserId = ShiroUtils.getUserId();
			return tccLocationTransferService.flowPass(id, loginUserId);
		} catch (Exception e) {
			return R.error(e.getMessage());
		}
	}

	/**
	 * 流转退回
	 */
	@RequiresPermissions("tccLocationTransfer:add")
	@PostMapping("/flowRefuse")
    @ResponseBody
	public R flowRefuse(HttpServletRequest request, Long id, String reason){
		try {
			return tccLocationTransferService.flowRefuse(id, reason, ShiroUtils.getUserId());
		} catch (Exception e) {
			return R.error(e.getMessage());
		}
	}

	/**
	 * 流转驳回
	 */
	@RequiresPermissions("tccLocationTransfer:add")
	@PostMapping("/flowInit")
	@ResponseBody
	public R flowInit(HttpServletRequest request, Long id, String reason){
		try {
			return tccLocationTransferService.flowInit(id, reason, ShiroUtils.getUserId());
		} catch (Exception e) {
			return R.error(e.getMessage());
		}
	}

	/**
	 * 删除
	 * @throws Exception
	 */
	@PostMapping("/deleteTrue")
    @ResponseBody
	public R deleteTrue(Long id){
		try {
			return tccLocationTransferService.deleteTrue(id);
		} catch (Exception e) {
			return R.error(e.getMessage());
		}
	}

	/*
	 * 导出
	 */
	@RequiresPermissions("tccLocationTransfer:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TccLocationTransfer cond, String conditions) throws Exception{

		cond = setSeachSql(cond, conditions);

		cond.setOrderby("A.Id DESC");

		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();

    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TCC_LocationTransfer)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();


        ExportExcelUtils<TccLocationTransfer> util = new ExportExcelUtils<TccLocationTransfer>(new TccLocationTransfer());

        String toFilePath = util.exportExcel(listData, listField, "货位转移单");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TccLocationTransfer setSeachSql(TccLocationTransfer cond, String conditions){

    	cond.addLeftJoin("TCC_LocationTransferItem LTI ON A.Bid = LTI.Pbid")
			.addField("LTI.stockControlBeforeId")//库存唯一
			.addField("LTI.transferNum")//转移数量
			.addField("LTI.assembleBid")//商品组装单号
			.addLeftJoin("TCC_StockControl AA ON AA.id = LTI.stockControlBeforeId")
			.addLeftJoin("TCC_Location SD ON SD.Chw = AA.Chw")//货位信息表
			.addField("SD.Vhw AS vhwYc")//货位
			.addLeftJoin("TCC_WarehouseArea SE ON SE.Ckq = SD.Ckq")//库区
			.addField("SE.Vkq AS vkqYc")//库区
			.addLeftJoin("TCC_Warehouse SF ON SF.Cck = SE.Cck")//仓库
			.addField("SF.Vck AS vckYc")//仓库
			.addField("AA.productName AS productName")//商品名称
			.addField("AA.productCode AS productCode")//商品编码
			.addField("AA.productNum AS productNum")//商品可用数量
			.addField("AA.productNumAll AS productNumAll")//商品总数量
			.addField("AA.drksj AS drksj")//入库时间
			.addField("AA.vgsbm AS vgsbm")//运营公司编码
			.addField("AA.vgsjc AS vgsjc")//运营公司简称

			.addLeftJoin("TCC_Location SDYR ON SDYR.Chw = LTI.Chw")//货位信息表
			.addField("SDYR.Vhw AS vhwYr")//移入货位
			.addLeftJoin("TCC_WarehouseArea SEYR ON SEYR.Ckq = SDYR.Ckq")//库区
			.addField("SEYR.Vkq AS vkqYr")//移入库区
			.addLeftJoin("TCC_Warehouse SFYR ON SFYR.Cck = SEYR.Cck")//仓库
			.addField("SFYR.Vck AS vckYr")//移入仓库

			.addLeftJoin("TP_ProductActive B on AA.productCode = B.Code")
			.addField("B.CodeMerge codeMerge")

			.addField("DATEDIFF(day, AA.Drksj, getdate()) kcdays")
			.addLeftJoin("TB_CompanyInfo SH ON SH.VId = AA.companyVid")
			.addField("SH.Vname AS companyName")//公司名称

			.addLeftJoin("YS_ProcInst P ON P.Bid = A.Bid ")
			.addField("FORMAT(P.UpdateTime, 'yyyy-MM-dd HH:mm:ss') AS auditingTime")//审核时间
		;

		cond = commonApiService.dataAuth(MENUID, cond);
        return  cond;
    }
}
